package com.xzit.day04.controller;

import com.github.pagehelper.PageInfo;
import com.xzit.day04.entity.Emp;
import com.xzit.day04.service.EmpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
//import io.swagger.annotations.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Tag(name = "员工管理", description = "员工信息的增删改查")
public class EmpController {

    @Autowired
    private EmpService empService;

    @PostMapping
    @Operation(summary = "新增员工")
    public void add(@RequestBody Emp emp) {
        empService.add(emp);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "按ID删除")
    public void delete(@PathVariable int id) {
        empService.deleteById(id);
    }

    @PostMapping("/deleteByCondition")
    @Operation(summary = "按条件删除")
    public void deleteByCondition(@RequestBody Emp emp) {
        empService.deleteByCondition(emp);
    }

    @PutMapping
    @Operation(summary = "更新员工")
    public void update(@RequestBody Emp emp) {
        empService.update(emp);
    }

    @GetMapping("/{id}")
    @Operation(summary = "按ID查询")
    public Emp findById(@PathVariable int id) {
        return empService.findById(id);
    }

    @GetMapping("/all")
    @Operation(summary = "查询全部")
    public List<Emp> findAll() {
        return empService.findAll();
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询全部")
    public PageInfo<Emp> pageAll(@RequestParam int pageNum, @RequestParam int pageSize) {
        return empService.findAllByPage(pageNum, pageSize);
    }

    @PostMapping("/pageByCondition")
    @Operation(summary = "按条件分页查询")
    public PageInfo<Emp> pageByCondition(@RequestBody Emp emp, @RequestParam int pageNum, @RequestParam int pageSize) {
        return empService.findByConditionByPage(emp, pageNum, pageSize);
    }
}
